from django.shortcuts import render

# Create your views here.
from rest_framework.generics import ListAPIView, CreateAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

from .paginations import MyPage
from .serializers import UserSerializer
from users.models import User


# 查询用户数据
class UserView(ListAPIView,CreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    pagination_class = MyPage

    def get_queryset(self):
        # 获取查询字符串参数keyword --> request.query_params.get("keyword")
        # 在非视图对象中获取请求对象 --> self.request 就是请求对象
        keyword = self.request.query_params.get('keyword')
        # 过滤
        if keyword is '' or keyword is None:
            return self.queryset.all()
        else:
            return self.queryset.filter(username__contains=keyword)

